package com.edward.action;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.edward.model.CommercialList;
import com.edward.model.ContractItem;
import com.edward.model.ContractList;
import com.edward.service.CommercialListManager;
import com.edward.service.ContractListManager;
import com.opensymphony.xwork2.ActionSupport;

public class PrintCmcAction extends ActionSupport {

	private static final long serialVersionUID = 7000562419610510545L;

	private List<ContractItem> list;

	private Map<String, Object> map;

	private CommercialListManager commercialListManager;

	private ContractListManager contractListManager;

	private CommercialList commercialList;

	private ContractItem contractItem;

	public List<ContractItem> getList() {
		return list;
	}

	public void setList(List<ContractItem> list) {
		this.list = list;
	}

	public ContractItem getContractItem() {
		return contractItem;
	}

	public void setContractItem(ContractItem contractItem) {
		this.contractItem = contractItem;
	}

	public CommercialList getCommercialList() {
		return commercialList;
	}

	public void setCommercialList(CommercialList conmercialList) {
		this.commercialList = conmercialList;
	}

	public void setContractListManager(ContractListManager contractListManager) {
		this.contractListManager = contractListManager;
	}

	public void setCommercialListManager(
			CommercialListManager commercialListManager) {
		this.commercialListManager = commercialListManager;
	}

	public Map<String, Object> getMap() {
		return map;
	}

	public void setMap(Map<String, Object> map) {
		this.map = map;
	}

	public String execute() throws Exception {
		CommercialList cmc = commercialListManager
				.getCommercialList(commercialList.getId());
		ContractList c = contractListManager.getContractList(cmc.getListNum());
		list = c.getItems();
		map = testReflect(cmc);
		map.put("hc", Math.ceil((c.getItems().size() + 1.0) / 2));
		return SUCCESS;
	}

	public static Map<String, Object> testReflect(Object obj) {
		Map<String, Object> hashMap = new HashMap<String, Object>();
		try {
			Class<? extends Object> c = obj.getClass();
			Method m[] = c.getDeclaredMethods();
			for (int i = 0; i < m.length; i++) {
				if (m[i].getName().indexOf("get") == 0) {
					hashMap
							.put(m[i].getName(), m[i]
									.invoke(obj, new Object[0]));
				}
			}
		} catch (Throwable e) {
			System.err.println(e);
		}
		return hashMap;
	}

}
